package com.sdleyou.treepro.common.service.impl;

import com.sdleyou.treepro.common.model.convert.BroadcastDO2BroadcastVO;
import com.sdleyou.treepro.common.model.dao.BroadcastDO;
import com.sdleyou.treepro.common.model.vo.BroadcastVO;
import com.sdleyou.treepro.common.service.UserBroadcastManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserBroadcastManagerImpl implements UserBroadcastManager {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<BroadcastVO> broadcasts() {
        Query query = new Query(Criteria.where("broad_id").ne(""));
        Sort sort = Sort.by(Sort.Direction.DESC, "gmt_create");
        query.with(sort).limit(20);
        List<BroadcastDO> broadcastDOList = mongoTemplate.find(query, BroadcastDO.class);

        return broadcastDOList.stream().map(e -> BroadcastDO2BroadcastVO.convert(e)).collect(Collectors.toList());
    }
}
